package com.czl.module_service.viewmodel

import android.text.TextUtils
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.RegexUtils
import com.czl.base.base.AppManager
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.RoomBean
import com.czl.base.data.bean.tengyun.HealthReportBean
import com.czl.base.data.bean.tengyun.HealthUserBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class AddHealthReportViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var userInfo = model.getLocalUserInfo()
    var areaName = model.getAreaName()
    val uc = UiChangeEvent()

    val onBirthdayClick: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.choiceBirthdayEvent.call()
    })

    val onSwitchAreaCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.switchAreaEvent.call()
    })

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<HealthUserBean.Data> = SingleLiveEvent()
        val loadRoomsEvent:SingleLiveEvent<List<RoomBean>> = SingleLiveEvent()
        val choiceBirthdayEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val switchAreaEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    }

    fun getAreaByUser() {
        model.getAreaByUser(model.getUserId())
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<HealthUserBean.Data>>(){
                override fun onResult(t: BaseBean<HealthUserBean.Data>) {
                    if (t.errorCode == 0){
                        uc.loadCompleteEvent.postValue(t.data)
                    }else{
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }
    fun getUserRooms() {
        model.getUserRooms(model.getLoginPhone().toString(),model.getAreaId())
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(
                object : ApiSubscriberHelper<BaseBean<List<RoomBean>>>() {
                    override fun onResult(t: BaseBean<List<RoomBean>>) {
                        if (t.code == 200) {
                            uc.loadRoomsEvent.postValue(t.data)
                            t.data?.let {
                                var rooms = Stream.of(it).map { e -> e.houseId + e.houseCode }
                                    .collect(Collectors.toList())
                                model.saveRoomIdAndCode(GsonUtils.toJson(rooms))
                            }

                        } else {
                            uc.loadRoomsEvent.postValue(null)
                            showErrorToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        uc.loadRoomsEvent.postValue(null)
                        showErrorToast(msg)
                    }

                }
            )
    }

    fun addHealthyReport(healthBean: HealthReportBean.Data) {
        healthBean.userId = model.getUserId()
        healthBean.neighId = model.getAreaId()
        LogUtils.e("---->" + healthBean)
        if (healthBean.healthyType == 2) {
            if (TextUtils.isEmpty(healthBean.suspected)) {
                showErrorToast("请选择疑似症状")
                return
            }
        }
        if (TextUtils.isEmpty(healthBean.temperature)) {
            showErrorToast("请填写体温")
            return
        } else {
            if (healthBean.temperature.toDoubleOrNull()!! <= 35 || healthBean.temperature.toDoubleOrNull()!! >= 42) {
                showErrorToast("请填写正确的体温")
                return
            }
        }
        if (healthBean.touching == 1) {
            if (TextUtils.isEmpty(healthBean.touchingTime) || TextUtils.isEmpty(healthBean.touchingName) || TextUtils.isEmpty(healthBean.touchingPhone)) {
                showErrorToast("请补充接触者信息")
                return
            }
            if (healthBean.touchingPhone.isNotBlank()) {
                if (!RegexUtils.isMobileSimple(healthBean.touchingPhone)) {
                    showErrorToast("接触人手机号格式错误")
                    return
                }
            }
        }
        if (healthBean.lifeDifficulties == 1) {
            if (TextUtils.isEmpty(healthBean.difficulties)) {
                showErrorToast("请选择困难类型")
                return
            }
        }
        model.addHealthReport(healthBean)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(
                object : ApiSubscriberHelper<BaseBean<Any?>>() {
                    override fun onResult(t: BaseBean<Any?>) {
                        showNormalToast("健康上报成功")
                        if (t.state == 0) {
                            LiveBusCenter.postAddHealthyCompleteEvent("")
                            AppManager.instance.finishActivity()
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showNormalToast("健康上报失败")
                    }

                }
            )
    }

}